import { isObjct, hasOwn } from "../../../until";
// import {
//   track
// } from "./track";
// import {
//   tigger
// } from "./tigger";
export const reactive = (target) => {
  if (!isObjct(target)) return target;
  const handler = {
    get(target, key, reciver) {
      // 收集依赖
      // track(target, key); //注释代码保证项目正常运行
      const result = Reflect.get(target, key, reciver);
      if (isObjct(result)) {
        return reactive(result);
      }
      return result;
    },
    set(target, key, value, reciver) {
      console.log(`设置对象属性${key}值`);
      const oldVal = Reflect.set(target, key, value, reciver);
      // set 是需要返回Boolean
      let result = true;
      // 根据新旧值是否相同来决定是否要更新
      if (oldVal !== value) {
        result = Reflect.set(target, key, value, reciver);
        // 触发更新
        // tigger(target, key); //注释代码保证项目正常运行
      }

      return result;
    },
    deleteProperty(target, key) {
      const hasKey = hasOwn(target, key);
      const result = Reflect.deleteProperty(target, key);
      if (result && hasKey) {
        // tigger(target, key);  //注释代码保证项目正常运行
      }
      return result;
    },
  };
  return new Proxy(target, handler);
};
